﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SetfNovelLib.Entities;
using System.Data.SqlClient;

namespace SetfNovelLib
{
    public class TestStef
    {
        public static BookInfo GenerateBook()
        {
            BookInfo res = new BookInfo();

            SqlConnection myConnection = new SqlConnection("user id=stefnovel-website;" +
                                       "password=180430;server=TURNSOL.arvixe.com;" +
                                       "Trusted_Connection=no;" +
                                       "database=stefnovel; " +
                                       "connection timeout=30");
            try
            {
                myConnection.Open();
            }
            catch(Exception e)
            {
                return null;
            }

            try
            {
                SqlDataReader myReader = null;
                SqlCommand myCommand = new SqlCommand("select * from ericmas001.TBooks where bookId = 1",
                                                         myConnection);
                myReader = myCommand.ExecuteReader();
                if (myReader.Read())
                {
                    res.Title = myReader["bookTitle"].ToString();
                }
                else
                    return null;
                myReader.Close();
           
                myCommand = new SqlCommand("select * from ericmas001.TChapters where bookId = 1",
                                                         myConnection);
                myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    ChapterInfo c = new ChapterInfo();
                    c.Title = myReader["chapterTitle"] == null ? "" : myReader["chapterTitle"].ToString();
                    c.TypeSection = (SectionType)myReader["chapterType"];
                    c.Content = myReader["chapterContent"] == null ? "" : myReader["chapterContent"].ToString();
                    if (c.TypeSection == SectionType.Developpement)
                    {
                        c.Id = myReader["chapterNumber"] == null ? -1 : (int)myReader["chapterNumber"];
                        res.ContentChapters.Add(c.Id, c);
                    }
                    else
                        res.SpecialSections.Add(c.TypeSection, c);
                }
                myReader.Close();
            }
            catch (Exception e)
            {
                return null;
            }

            return res;
        }
    }
}